<?php
/**
 * Created by PhpStorm.
 * User: Bobo
 * 获取OpenID
 * Date: 2017/4/12
 * Time: 16:10
 */
//exit('sb');
ini_set('display_errors',1);            //错误信息
ini_set('display_startup_errors',1);    //php启动错误信息
error_reporting(-1);

/**
 * appid：wxe793a93890da6037
密钥：f3d90b3f6de6407f025283cf3adf5631
 */
define('APPID','wxe793a93890da6037');
define('SECRETKEY','f3d90b3f6de6407f025283cf3adf5631');

//define('APPID','wxfc58831ca6d6f156');
//define('SECRETKEY','a0bf0bf22b1dc198df1b21fc50288aac');
// 365 页游
//define('APPID','wxc41cd197a1e2a6da');
//define('SECRETKEY','d621dd8727647166f3e11c2336688edd');

//const APPID = 'wxc41cd197a1e2a6da';
//const MCHID = '1447838802';
//const KEY = 'de494172dbb57285ca1199c97d4f5237';
//const APPSECRET = 'd621dd8727647166f3e11c2336688edd';

ini_set('date.timezone','Asia/Shanghai');
$token_key = md5(APPID);
$token_file = 'cat/'.APPID.'.acc_token';
$openId = '';
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
//    if (!file_exists($token_file)){ // 不存在token 则获取token
//        if (!isset($_GET['code'])) { // 并没有设置
//            goWXUrl();
//        }
//        $code = $_GET['code'];
//        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.APPID.'&secret='.SECRETKEY.'&code='.$code.'&grant_type=authorization_code';
//    } else {
//        $url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.APPID.'&grant_type=refresh_token&refresh_token='.file_get_contents($token_file);
//    }
    
    if (!isset($_GET['code'])) { // 并没有设置
        goWXUrl();
    }
    $code = $_GET['code'];
    $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.APPID.'&secret='.SECRETKEY.'&code='.$code.'&grant_type=authorization_code';
    
    $rst = file_get_contents($url);
    $rst = json_decode($rst, true);
    if (isset($rst['access_token'])) {
        $openId = $rst['openid'];
//        echo '<h1>OpenId: '.$rst['openid'].'</h1>';
//        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$rst['access_token'].'&openid='.$rst['openid'].'&lang=zh_CN';
//        $info = json_decode(file_get_contents($url), true);
    } else { // 获取token出错
        goWXUrl();
    }
} else {
    exit('not weixin');
}

function goWXUrl (){
    $time = time();
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url =  "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $url = getWXUrl($url);
    header('Location:'.$url);exit();
}

/**
 * @param $url
 * @param string $type snsapi_userinfo 获取用户信息 snsapi_base  获取openID
 * @return string
 */
function getWXUrl ($url, $type = 'snsapi_base'){
    return 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.APPID.'&redirect_uri='.urlencode($url).'&response_type=code&scope='.$type.'&state=1#wechat_redirect';
}

if (empty($openId)) {exit('error');}

header('Location:https://pay.szwzpay.com/pay/weifutong.html?openid='.$openId);
//header('Location:https://www.h5youx.com/game/test/weifutong.html?openid='.$openId);
exit();

?><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>pay</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>
<style>
</style>
<body>
<h1>支付测试</h1>

<div>
    <label for="appID">appID:</label>
    <input id="appID" name="appID" type="text" value="P806DACB0A00C5D4B3B330FFDDB2B68A4">
</div>
<div>
    <label for="openKey">openKey:</label>
    <input id="openKey" name="openKey" type="text" value="0845e708997ee18d11c5fb20d0a142c1">
</div>
<div>
    <label for="openid">openid(选填):</label>
    <input id="openid" name="openid" type="text" value="<?php echo $openId;?>">
</div>
<div>
    <label for="userName">userName:</label>
    <input id="userName" name="userName" type="text" value="admin@dataeye.com">
</div>
<div>
    <label for="productNo">productNo:</label>
    <input id="productNo" name="productNo" type="text" value="P000040">
</div>
<div>
    <label for="amount">amount(金额分):</label>
    <input id="amount" name="amount" type="text" value=10>
</div>
<div>
    <label for="returnurl">returnurl:</label>
    <input id="returnurl" name="returnurl" type="text" value="http://nba.qq.com">
</div>
</div>
<button onclick="paysome()" style="width: 200px; height: 50px;">支付</button>
<script src="zepto.min.js"></script>
<script src="https://pay.szwzpay.com/pay/paysdk.min.js?v=1732"></script>
<script src="https://www.h5youx.com/game/Game/Home/View/default/Public/js/vconsole.min.js"></script>
<script>
    // appID 和 openKey 由优点支付管理后台获取
    var appID = $('#appID').val()
    var openKey = $('#openKey').val()
    var openid = $('#openid').val()
    // 加载页面需要先初始化支付
    // 'W'微信公众号， 'Q' QQ公众号（暂不支持）
    var paysdk = new Paysdk(appID, openKey, 'W', openid)
    paysdk.url = 'https://pay.szwzpay.com'
    paysdk.initPaysdk()
    function paysome() {
        paysdk.openid = $('#openid').val()
        var timestamp = Date.parse(new Date()) + Math.floor(Math.random()*100)
        // orderNo 订单号不可重复
        // userName 用户名
        // productNo 管理后台产品编号
        // amount （分），管理后台设置应用传入价格必须设定amount，平台设定价格可不填，若填amount时，会覆盖平台设定的价格
        // returnurl 支付完成的回调地址
        var orderParams = {
            orderNo: timestamp,
            userName: $('#userName').val(),
            productNo: $('#productNo').val(),
            amount: $('#amount').val(),
            returnurl: $('#returnurl').val(),
        }
        paysdk.pay(orderParams)
    }
</script>
</body>
</html>


